package cn.edu.nju.ws.sview.wyge.relations.adataset;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import cn.edu.nju.ws.sview.cache.URIIDCache;
import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.database.DBOperations;
import cn.edu.nju.ws.sview.util.PropertyList;
/**
 * 
 * @author Weiyi Ge
 */
public class DocumentDataset {
	private String name = null;
	private int id = -1;
	private int contributorId = -1;
	private String date = null;

	public DocumentDataset(int id, String name) {
		this.name = name;
		this.id = id;
	}

	public static DocumentDataset getInstance(int id) {
		String name = null;
		try {
			name = URIIDCache.getInstance().getURI(id);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new DocumentDataset(id, name);
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getID() {
		return id;
	}

	public String getName() {
		return name;
	}
	public int getContributorId(){
		if(this.contributorId<0){
			int ctmpId = -1;
			if (this.id < 0) {
				return ctmpId;
			}
			try {
				ArrayList<String> properties = PropertyList.getContributorProperties();
				ArrayList<String> array = new ArrayList<String>();
				for(String str:properties){
					int p = DBOperations.getURIID(str,true) ;
					array.add("u"+p) ;
				}
				
	//			int s = DBOperations.getURIID(uri,false);
	//			int doc_id = DBOperations.getDereferenceDocID(uri);
				Connection conn = DBConnectionFactory.getConnection();
				PreparedStatement stmt = conn.prepareStatement("SELECT o FROM quadruple WHERE doc_id=? and p=?  and o like 'u%'");
				stmt.setInt(1, this.id);
				for(String property:array){
					stmt.setString(2, property) ;
					ResultSet rs = stmt.executeQuery();
					if(rs.next()){
						ctmpId = Integer.parseInt(rs.getString("o").substring(1));
//						contributorStr = URIIDCache.getInstance().getURI(locationID);
						break;
					}
					rs.close();
				}
				stmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.contributorId = ctmpId;	
		}
		return this.contributorId;
	}
	
	public String getDate(){
		if(this.date == null){
			String dateStr = "";
			if (this.id < 0) {
				return dateStr;
			}
			try {
				ArrayList<String> properties = PropertyList.getDateProperties();//PropertyList.getContributorProperties();
				ArrayList<String> array = new ArrayList<String>();
				for(String str:properties){
					int p = DBOperations.getURIID(str,true);
					array.add("u"+p) ;
				}
				
	//			int s = DBOperations.getURIID(uri,false);
	//			int doc_id = DBOperations.getDereferenceDocID(uri);
				Connection conn = DBConnectionFactory.getConnection();
				PreparedStatement stmt = conn.prepareStatement("SELECT o FROM quadruple WHERE doc_id=? and p=?  and o like 'l%'");
				stmt.setInt(1, this.id);
				for(String property:array){
//					System.out.println("prop:"+property);
					stmt.setString(2, property) ;
					ResultSet rs = stmt.executeQuery();
					if(rs.next()){
						dateStr = rs.getString(1).substring(1);
//						System.out.println("date:"+dateStr);
						break;
					}
					rs.close();
				}
				stmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.date = dateStr;	
		}
		return this.date;
	}
//	public static ArrayList<String> getTopicsById(int uriID, String lang){
//		System.out.println("in DocumentDataset.getTopicsById:"+uriID+"|"+lang);
//		ArrayList<String> topics = new ArrayList<String>();
//		ArrayList<String> ptopics = PropertyList.getTopicProperties();
//		ArrayList<String> parray = new ArrayList<String>();
//		try {
//			for(String puri : ptopics){
//				int pid = DBOperations.getURIID(puri, true);	
//				parray.add("u"+pid);
//			}	
//			Connection conn = DBConnectionFactory.getConnection();
//			PreparedStatement stmt = conn.prepareStatement("SELECT o FROM quadruple WHERE doc_id=? and p=? and o like 'u%' ");
//			stmt.setInt(1, uriID);
//			ResultSet rs = null;
//			for(String p : parray){
//				stmt.setString(2, p);
//				rs = stmt.executeQuery();
//				if(rs.next()){
//					topics.add(rs.getString(1).substring(1));//=====
//					System.out.println("addp:"+rs.getString(1).substring(1));
//				}
//				rs.close();
//			}
//		} catch (Throwable e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return topics;
//	}
}
